home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / trim_lib / trim_aux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-11  |  23.0 KB  |  475 lines

  1. /******************************************************************************
  2. * Trim_aux.c - auxiliary routine to interface to different free from types.   *
  3. *******************************************************************************
  4. * Written by Gershon Elber, July. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include "trim_loc.h"
  8.  
  9. #define VEC_FIELD_TRIES    10
  10. #define VEC_FIELD_START_STEP 1e-6
  11.  
  12. CagdBType _TrimEuclidComposedFromUV = FALSE;
  13.  
  14. /*****************************************************************************
  15. * DESCRIPTION:                                                               M
  16. *   Returns the parametric domain of a trimmed surface.                 M
  17. *                                                                            *
  18. * PARAMETERS:                                                                M
  19. *   TrimSrf:   To get its parametric domain.                                 M
  20. *   UMin:      Where to put the minimal U domain's boundary.                 M
  21. *   UMax:      Where to put the maximal U domain's boundary.                 M
  22. *   VMin:      Where to put the minimal V domain's boundary.                 M
  23. *   VMax:      Where to put the maximal V domain's boundary.                 M
  24. *                                                                            *
  25. * RETURN VALUE:                                                              M
  26. *   void                                                                     M
  27. *                                                                            *
  28. * KEYWORDS:                                                                  M
  29. *   TrimSrfDomain, domain, parametric domain                                 M
  30. *****************************************************************************/
  31. void TrimSrfDomain(TrimSrfStruct *TrimSrf,
  32.            CagdRType *UMin,
  33.            CagdRType *UMax,
  34.            CagdRType *VMin,
  35.            CagdRType *VMax)
  36. {
  37.     CagdSrfDomain(TrimSrf -> Srf, UMin, UMax, VMin, VMax);
  38. }
  39.  
  40. /*****************************************************************************
  41. * DESCRIPTION:                                                               M
  42. * Given a trimmed surface and parameter values u, v, evaluate the surface at M
  43. * (u, v).                                     M
  44. *                                                                            *
  45. * PARAMETERS:                                                                M
  46. *   TrimSrf: To evaluate at the given parametric location (u, v).            M
  47. *   u, v:    The parameter values at which TrimSrf is to be evaluated.       M
  48. *                                                                            *
  49. * RETURN VALUE:                                                              M
  50. *   CagdRType *: A vector holding all the coefficients of all components     M
  51. *                of surface TrimSrf's point type. If, for example, TrimSrf's M
  52. *                point type is P2, the W, X, and Y will be saved in the      M
  53. *                first three locations of the returned vector. The first     M
  54. *                location (index 0) of the returned vector is reserved for   M
  55. *                the rational coefficient W and XYZ always starts at second  M
  56. *                location of the returned vector (index 1).                  M
  57. *                                                                            *
  58. * KEYWORDS:                                                                  M
  59. *   TrimSrfEval, evaluation                                                  M
  60. *****************************************************************************/
  61. CagdRType *TrimSrfEval(TrimSrfStruct *TrimSrf, CagdRType u, CagdRType v)
  62. {
  63.     return CagdSrfEval(TrimSrf -> Srf, u, v);
  64. }
  65.  
  66. /*****************************************************************************
  67. * DESCRIPTION:                                                               M
  68. * Returns a new trimmed surface representing the same surface as TrimSrf but M
  69. * with its degree raised by one.                         M
  70. *                                                                            *
  71. * PARAMETERS:                                                                M
  72. *   TrimSrf:       To raise its degree.                                      M
  73. *                                                                            *
  74. * RETURN VALUE:                                                              M
  75. *   TrimSrfStruct *:  A surface with same geometry as Srf but with one       M
  76. *                     degree higher.                                         M
  77. *                                                                            *
  78. * KEYWORDS:                                                                  M
  79. *   TrimSrfDegreeRaise, degree raising                                       M
  80. *****************************************************************************/
  81. TrimSrfStruct *TrimSrfDegreeRaise(TrimSrfStruct *TrimSrf, CagdSrfDirType Dir)
  82. {
  83.     return TrimSrfNew(CagdSrfDegreeRaise(TrimSrf -> Srf, Dir),
  84.               TrimCrvCopyList(TrimSrf -> TrimCrvList),
  85.               FALSE);
  86. }
  87.  
  88. /*****************************************************************************
  89. * DESCRIPTION:                                                               M
  90. * Given a trimmed surface - subdivides it into two sub-surfaces at given     M
  91. * parametric value t in the given direction Dir.                             M
  92. *    Returns pointer to a list of two trimmed surfaces, at most. It can very M
  93. * well may happen that the subdivided surface is completely trimmed out and  M
  94. * hence nothing is returned for it.                         M
  95. *                                                                            *
  96. * PARAMETERS:                                                                M
  97. *   TrimSrf:  To subdivide at the prescibed parameter value t.               M
  98. *   t:        The parameter to subdivide the curve Crv at.                   M
  99. *   Dir:      Direction of subdivision. Either U or V.                       M
  100. *                                                                            *
  101. * RETURN VALUE:                                                              M
  102. *   TrimSrfStruct *:  The subdivided surfaces. Usually two, but can have     M
  103. *              only one, if other is totally trimmed away.            M
  104. *                                                                            *
  105. * KEYWORDS:                                                                  M
  106. *   TrimSrfSubdivAtParam, subdivision                                        M
  107. *****************************************************************************/
  108. TrimSrfStruct *TrimSrfSubdivAtParam(TrimSrfStruct *TrimSrf,
  109.                     CagdRType t,
  110.                     CagdSrfDirType Dir)
  111. {
  112.     fprintf(stderr, "TrimSrfSubdivAtParam not implemented\n");
  113.     return NULL;
  114. }
  115.  
  116. /*****************************************************************************
  117. * DESCRIPTION:                                                               M
  118. * Given a trimmed surface - extracts a sub-region within the domain          M
  119. * specified by t1 and t2, in the direction Dir.                              M
  120. *                                                                            *
  121. * PARAMETERS:                                                                M
  122. *   TrimSrf:   To extract a sub-region from.                                 M
  123. *   t1, t2:    Parametric domain boundaries of sub-region.                   M
  124. *   Dir:       Direction of region extraction. Either U or V.                M
  125. *                                                                            *
  126. * RETURN VALUE:                                                              M
  127. *   TrimSrfStruct *:  Sub-region extracted from TrimSrf from t1 to t2.       M
  128. *                                                                            *
  129. * KEYWORDS:                                                                  M
  130. *   TrimSrfRegionFromTrimSrf, regions, subdivision                           M
  131. *****************************************************************************/
  132. TrimSrfStruct *TrimSrfRegionFromTrimSrf(TrimSrfStruct *TrimSrf,
  133.                     CagdRType t1,
  134.                     CagdRType t2,
  135.                     CagdSrfDirType Dir)
  136. {
  137.     CagdRType TMin, TMax, R1, R2;
  138.     TrimSrfStruct *TrimSrfs;
  139.     CagdBType
  140.     OpenEnd = FALSE,
  141.     NewTrimSrf = FALSE;
  142.  
  143.     if (Dir == CAGD_CONST_U_DIR)
  144.     TrimSrfDomain(TrimSrf, &TMin, &TMax, &R1, &R2);
  145.     else
  146.     TrimSrfDomain(TrimSrf, &R1, &R2, &TMin, &TMax);
  147.     CAGD_DOMAIN_T_VERIFY(t1, TMin, TMax);
  148.     CAGD_DOMAIN_T_VERIFY(t2, TMin, TMax);
  149.  
  150.     if (t1 > t2)
  151.     SWAP(CagdRType, t1, t2);
  152.  
  153.     if (!APX_EQ(t1, TMin) || !OpenEnd) {
  154.     TrimSrfs = TrimSrfSubdivAtParam(TrimSrf, t1, Dir);
  155.     TrimSrf = TrimSrfs -> Pnext;
  156.     TrimSrfs -> Pnext = NULL;
  157.     if (TRIM_IS_FIRST_SRF(TrimSrfs))
  158.         TrimSrfFree(TrimSrfs);           /* Free the first region. */
  159.     if (TrimSrf == NULL)
  160.         return NULL;       /* No second region -completely trimmed away. */
  161.     NewTrimSrf = TRUE;
  162.     }
  163.  
  164.     if (APX_EQ(t2, TMax) && OpenEnd)
  165.     return NewTrimSrf ? TrimSrf : TrimSrfCopy(TrimSrf);
  166.     else {
  167.     TrimSrfs = TrimSrfSubdivAtParam(TrimSrf, t2, Dir);
  168.  
  169.     if (NewTrimSrf)
  170.         TrimSrfFree(TrimSrf);
  171.  
  172.         if (TrimSrfs -> Pnext != NULL)
  173.         TrimSrfFree(TrimSrfs -> Pnext);      /* Free the second region. */
  174.         TrimSrfs -> Pnext = NULL;
  175.     return TrimSrfs;            /* Returns the first region. */
  176.     }
  177. }
  178. /*****************************************************************************
  179. * DESCRIPTION:                                                               M
  180. * Given a trimmed surface - refines it at the given n knots as defined by    M
  181. * vector t.                                     M
  182. *   If Replace is TRUE, the values in t replaces current knot vector.         M
  183. *   Returns pointer to refined surface (Note a Bezier surface will be        M
  184. * converted into a Bspline surface).                                         M
  185. *                                                                            *
  186. * PARAMETERS:                                                                M
  187. *   TrimSrf:   To refine.                                                    M
  188. *   Dir:       Direction of refinement. Either U or V.                       M
  189. *   Replace:   If TRUE, t holds knots in exactly the same length as the      M
  190. *              length of the knot vector of Srf and t simply replaces the    M
  191. *              knot vector.                                                  M
  192. *   t:         Vector of knots with length of n.                             M
  193. *   n:         Length of vector t.                                           M
  194. *                                                                            *
  195. * RETURN VALUE:                                                              M
  196. *   TrimSrfStruct *:  A refined surface of TrimSrf after insertion of all    M
  197. *                     the knots as specified by vector t of length n.        M
  198. *                                                                            *
  199. * KEYWORDS:                                                                  M
  200. *   TrimSrfRefineAtParams, refinement, subdivision                           M
  201. *****************************************************************************/
  202. TrimSrfStruct *TrimSrfRefineAtParams(TrimSrfStruct *TrimSrf,
  203.                      CagdSrfDirType Dir,
  204.                      CagdBType Replace,
  205.                      CagdRType *t,
  206.                      int n)
  207. {
  208.     return TrimSrfNew(CagdSrfRefineAtParams(TrimSrf -> Srf, Dir, Replace, t, n),
  209.               TrimCrvCopyList(TrimSrf -> TrimCrvList), FALSE);
  210. }
  211.  
  212. /*****************************************************************************
  213. * DESCRIPTION:                                                               M
  214. * Returns a new trimmed surface that is the reversed surface of TrimSrf by   M
  215. * reversing the control mesh and the knot vector (if Bspline surface) of     M
  216. * TrimSrf in the U direction, as well as its trimming curves. See also         M
  217. * CagdSrfReverse and BspKnotReverse.                                   M
  218. *                                                                            *
  219. * PARAMETERS:                                                                M
  220. *   TrimSrf:       To be reversed.                                           M
  221. *                                                                            *
  222. * RETURN VALUE:                                                              M
  223. *   TrimSrfStruct *:   Reversed surface of TrimSrf.                          M
  224. *                                                                            *
  225. * KEYWORDS:                                                                  M
  226. *   TrimSrfReverse, reverse                                                  M
  227. *****************************************************************************/
  228. TrimSrfStruct *TrimSrfReverse(TrimSrfStruct *TrimSrf)
  229. {
  230.     CagdRType UMin, UMax, VMin, VMax;
  231.     TrimCrvStruct *TrimCrv,
  232.     *TrimCrvList = TrimCrvCopyList(TrimSrf -> TrimCrvList);
  233.  
  234.     TrimSrfDomain(TrimSrf, &UMin, &UMax, &VMin, &VMax);
  235.  
  236.     for (TrimCrv = TrimCrvList; TrimCrv != NULL; TrimCrv = TrimCrv -> Pnext) {
  237.     TrimCrvSegStruct *TrimCrvSeg;
  238.  
  239.     for (TrimCrvSeg = TrimCrv -> TrimCrvSegList;
  240.          TrimCrvSeg != NULL;
  241.          TrimCrvSeg = TrimCrvSeg -> Pnext) {
  242.         int i,
  243.         Length = TrimCrvSeg -> UVCrv -> Length;
  244.         CagdRType
  245.         **Points = TrimCrvSeg -> UVCrv -> Points,
  246.         *UPts = Points[1];
  247.  
  248.         if (TrimCrvSeg -> UVCrv -> PType != CAGD_PT_E2_TYPE)
  249.         TRIM_FATAL_ERROR(TRIM_ERR_TRIM_CRV_E2);
  250.         for (i = 0; i < Length; i++)
  251.         UPts[i] = UMax - (UPts[i] - UMin);
  252.     }
  253.     }
  254.  
  255.     return TrimSrfNew(CagdSrfReverse(TrimSrf -> Srf), TrimCrvList, FALSE);
  256. }
  257.  
  258. /*****************************************************************************
  259. * DESCRIPTION:                                                               M
  260. * Returns a new trimmed surface that is the reversed surface of Srf by       M
  261. * flipping the U and the V directions of the surface, as well as flipping    M
  262. * them in the trimming curves.                             M
  263. * See also BspKnotReverse.                                             M
  264. *                                                                            *
  265. * PARAMETERS:                                                                M
  266. *   TrimSrf:       To be reversed.                                           M
  267. *                                                                            *
  268. * RETURN VALUE:                                                              M
  269. *   TrimSrfStruct *:   Reversed surface of TrimSrf.                          M
  270. *                                                                            *
  271. * KEYWORDS:                                                                  M
  272. *   TrimSrfReverse, reverse                                                  M
  273. *****************************************************************************/
  274. TrimSrfStruct *TrimSrfReverse2(TrimSrfStruct *TrimSrf)
  275. {
  276.     TrimCrvStruct *TrimCrv,
  277.     *TrimCrvList = TrimCrvCopyList(TrimSrf -> TrimCrvList);
  278.  
  279.     for (TrimCrv = TrimCrvList; TrimCrv != NULL; TrimCrv = TrimCrv -> Pnext) {
  280.     TrimCrvSegStruct *TrimCrvSeg;
  281.  
  282.     for (TrimCrvSeg = TrimCrv -> TrimCrvSegList;
  283.          TrimCrvSeg != NULL;
  284.          TrimCrvSeg = TrimCrvSeg -> Pnext) {
  285.         int i,
  286.         Length = TrimCrvSeg -> UVCrv -> Length;
  287.         CagdRType
  288.         **Points = TrimCrvSeg -> UVCrv -> Points,
  289.         *UPts = Points[1],
  290.         *VPts = Points[2];
  291.  
  292.         if (TrimCrvSeg -> UVCrv -> PType != CAGD_PT_E2_TYPE)
  293.         TRIM_FATAL_ERROR(TRIM_ERR_TRIM_CRV_E2);
  294.         for (i = 0; i < Length; i++)
  295.         SWAP(CagdRType, UPts[i], VPts[i]);
  296.     }
  297.     }
  298.  
  299.     return TrimSrfNew(CagdSrfReverse2(TrimSrf -> Srf), TrimCrvList, FALSE);
  300. }
  301.  
  302. /*****************************************************************************
  303. * DESCRIPTION:                                                               M
  304. *   Extracts the trimming curves of the given trimmed surface.               M
  305. *                                                                            *
  306. * PARAMETERS:                                                                M
  307. *   TrimSrf:       Trimmed surface to extract trimming curves from.          M
  308. *   ParamSpace:    TRUE for curves in parameteric space, FALSE of 3D         M
  309. *           Euclidean space.                         M
  310. *                                                                            *
  311. * RETURN VALUE:                                                              M
  312. *   CagdCrvStruct *:   List of trimming curves of TrimSrf.                   M
  313. *                                                                            *
  314. * KEYWORDS:                                                                  M
  315. *   TrimGetTrimmingCurves, trimming curves                                   M
  316. *****************************************************************************/
  317. CagdCrvStruct *TrimGetTrimmingCurves(TrimSrfStruct *TrimSrf,
  318.                      CagdBType ParamSpace)
  319. {
  320.     CagdCrvStruct
  321.     *NewTrimCrvList = NULL;
  322.     TrimCrvStruct
  323.     *TrimCrvList = TrimSrf -> TrimCrvList;
  324.  
  325.     for ( ; TrimCrvList != NULL; TrimCrvList = TrimCrvList -> Pnext) {
  326.     TrimCrvSegStruct
  327.         *TrimCrvSegList = TrimCrvList -> TrimCrvSegList;
  328.  
  329.     for (;
  330.          TrimCrvSegList != NULL;
  331.          TrimCrvSegList = TrimCrvSegList -> Pnext) {
  332.         CagdCrvStruct *TCrv;
  333.  
  334.         if (ParamSpace)
  335.         TCrv = CagdCrvCopy(TrimCrvSegList -> UVCrv);
  336.         else {
  337.         if (TrimCrvSegList -> EucCrv == NULL) {
  338.             TrimCrvSegList -> EucCrv =
  339.                 TrimEvalTrimCrvToEuclid(TrimSrf,
  340.                         TrimCrvSegList -> UVCrv);
  341.         }
  342.         TCrv = CagdCrvCopy(TrimCrvSegList -> EucCrv);
  343.         }
  344.         LIST_PUSH(TCrv, NewTrimCrvList);
  345.     }
  346.     }
  347.  
  348.     return NewTrimCrvList;
  349. }
  350.  
  351. /*****************************************************************************
  352. * DESCRIPTION:                                                               M
  353. *   Routine to convert the trimming curves of a trimmed surface to polylines.M
  354. *   Polyline are always E3 of CagdPolylineStruct type.                 M
  355. *   NULL is returned in case of an error, otherwise list of                  M
  356. * CagdPolylineStruct.                                  M
  357. *                                                                            *
  358. * PARAMETERS:                                                                M
  359. *   TrimSrf:           To extract isoparametric curves from.                 M
  360. *   ParamSpace:        TRUE for curves in parameteric space, FALSE of 3D     M
  361. *               Euclidean space.                         M
  362. *   SamplesPerCurve:   Fineness control on piecewise linear curve            M
  363. *                      approximation.                                        M
  364. *   Optimal:           Use optimal approximation of isocurves.             M
  365. *                                                                            *
  366. * RETURN VALUE:                                                              M
  367. *   CagdPolylineStruct *: List of polylines representing a piecewise linear  M
  368. *                         approximation of the extracted isoparamteric       M
  369. *                         curves or NULL is case of an error.                M
  370. *                                                                            *
  371. * KEYWORDS:                                                                  M
  372. *   TrimCrvs2Polylines, trimming curves                                  M
  373. *****************************************************************************/
  374. CagdPolylineStruct *TrimCrvs2Polylines(TrimSrfStruct *TrimSrf,
  375.                        CagdBType ParamSpace,
  376.                        int SamplesPerCurve,
  377.                        int Optimal)
  378. {
  379.     CagdCrvStruct *Crv,
  380.         *Crvs = TrimGetTrimmingCurves(TrimSrf, ParamSpace);
  381.     CagdPolylineStruct *Poly,
  382.     *Polys = NULL;
  383.  
  384.     for (Crv = Crvs; Crv != NULL; Crv = Crv -> Pnext) {
  385.     Poly = SymbCrv2Polyline(Crv, SamplesPerCurve, Optimal, TRUE);
  386.     Poly -> Pnext = Polys;
  387.     Polys = Poly;
  388.     }
  389.  
  390.     CagdCrvFreeList(Crvs);
  391.     return Polys;    
  392. }
  393.  
  394. /*****************************************************************************
  395. * DESCRIPTION:                                                               M
  396. *   Computes the composed Euclidean curve of TrimSrf(UVCrv).             M
  397. * The resulting curve is either computed using a piecewise linear            M
  398. * approximation or by symbolically composing it onto the surface.            M
  399. *                                                                            *
  400. * PARAMETERS:                                                                M
  401. *   TrimSrf:    To compute the Euclidean UVCrv for.                          M
  402. *   UVCrv:      A curve in the parametric space of TrimSrf.                  M
  403. *                                                                            *
  404. * RETURN VALUE:                                                              M
  405. *   CagdCrvStruct *:  A Euclidean curve in TrimSrf, following UVCrv.         M
  406. *                                                                            *
  407. * KEYWORDS:                                                                  M
  408. *   TrimEvalTrimCrvToEuclid                                                  M
  409. *****************************************************************************/
  410. CagdCrvStruct *TrimEvalTrimCrvToEuclid(TrimSrfStruct *TrimSrf,
  411.                        CagdCrvStruct *UVCrv)
  412. {
  413.     if (_TrimEuclidComposedFromUV) {
  414.     return SymbComposeSrfCrv(TrimSrf -> Srf, UVCrv);
  415.     }
  416.     else {
  417.     int i, j,
  418.         MaxAxis = CAGD_NUM_OF_PT_COORD(TrimSrf -> Srf -> PType),
  419.         IsRational = CAGD_IS_RATIONAL_PT(TrimSrf -> Srf -> PType);
  420.     CagdPolylineStruct
  421.         *UVPoly = SymbCrv2Polyline(UVCrv, _TrimUVSamplesPerCurve,
  422.                               _TrimUVSamplesOptimal, FALSE);
  423.     CagdCrvStruct
  424.         *EucCrv = CagdCrvNew(UVCrv -> GType, 
  425.                  TrimSrf -> Srf -> PType, 
  426.                  UVPoly -> Length);
  427.     CagdRType
  428.         **Points = EucCrv -> Points;
  429.  
  430.     EucCrv -> Order = 2;
  431.     if (CAGD_IS_BSPLINE_CRV(UVCrv)) {
  432.         EucCrv -> KnotVector = BspKnotUniformOpen(UVPoly -> Length,
  433.                               2, NULL);
  434.     }
  435.  
  436.     for (i = 0; i < EucCrv -> Length; i++) {
  437.         CagdRType
  438.         *R = CagdSrfEval(TrimSrf -> Srf, UVPoly -> Polyline[i].Pt[0],
  439.                          UVPoly -> Polyline[i].Pt[1]);
  440.  
  441.         for (j = !IsRational; j <= MaxAxis; j++)
  442.             Points[j][i] = R[j];
  443.     }
  444.  
  445.     return EucCrv;
  446.     }
  447. }
  448.  
  449. /*****************************************************************************
  450. * DESCRIPTION:                                                               M
  451. *   Sets the way Euclidean trimming curves are computed from parametric      M
  452. * trimming curves. Either by symbolic composition (TRUE) or by piecewise     M
  453. * linear approximation of trimming curves (FALSE).                 M
  454. *                                                                            *
  455. * PARAMETERS:                                                                M
  456. *   EuclidComposedFromUV:   Do we want symbolic composition for Euclidean    M
  457. *        curves, or should we piecewise linear sample the UV trimming M
  458. *        curves.                                 M
  459. *                                                                            *
  460. * RETURN VALUE:                                                              M
  461. *   int:  Old value of way of Euclidean curve's computation                  M
  462. *                                                                            *
  463. * KEYWORDS:                                                                  M
  464. *   TrimSetEuclidComposedFromUV                                              M
  465. *****************************************************************************/
  466. int TrimSetEuclidComposedFromUV(int EuclidComposedFromUV)
  467. {
  468.     int OldEuclidComposedFromUV = _TrimEuclidComposedFromUV;
  469.  
  470.     _TrimEuclidComposedFromUV = EuclidComposedFromUV;
  471.  
  472.     return OldEuclidComposedFromUV;
  473. }
  474.  
  475.